(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var dataCache = [];
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initTime();
			initNews(currentPage,pageSize);
		});
	});
	function initTime(){
		var date = new Date();
		var newDate = getFormatDate(date,"yyyy-MM-dd hh:mm");
		var startTime = getFormatDate(new Date(date.getTime()-1000*60*60*24),"yyyy-MM-dd ");
		$("#anews-starttime").val(startTime+"00:00");
		$("#anews-endtime").val(newDate.substring(0,11)+"23:59");
		
//		$("#anews-starttime").val(getSmpFormatDate(new Date(new Date()- 24 * 60 * 60 * 1000 * 2),false));
//		$("#anews-endtime").val(getSmpFormatDate(new Date(),false));
	}
	function initNews(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var type = $("#anews-select option:selected").val();
		var starttime = strToDate($("#anews-starttime").val())/1000;
		var endtime = strToDate($("#anews-endtime").val())/1000;
		var data = {
				cmd:"getMessage",
				model:"teacher",
				userKey:userKey,
				data:{
					currentpage:currentpage,
					pagesize:pagesize,
					type:type,
					starttime:starttime,
					endtime:endtime,
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				var total = res.data[0].total;
				totalSize = Math.ceil(total/pagesize);
				if(totalSize <= 5 && totalSize >0){
					for(var i = totalSize-1 ; i >= 0; i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}else if(totalSize == 0){
					pagecontent += "<div class=\"pages-btn\">1</div>";
				}else if(totalSize > 5){
					var lastp = (now*5)+5;
					if(lastp > totalSize){
						lastp = totalSize;
					}
					for(var i = lastp-1 ; i >= (now*5); i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}
				pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
				$("#anews-page").empty();
				$("#anews-page").append(pagecontent);
				
				$(".pages-btn").unbind("click");
				$(".pages-btn").click(function(){
					dataCache = [];
					$(".pages-btn").removeClass("page-btn-select");
					$(this).addClass("page-btn-select");
					currentPage = parseInt($(this).text());
					initNews(currentPage,pageSize);
				});
				
				$(".pages-btn-last").unbind("click");
				$(".pages-btn-last").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(now > 0){
							now --;
							initNews((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				$(".pages-btn-next").unbind("click");
				$(".pages-btn-next").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(totalSize > (now+1)*5){
							now ++;
							initNews((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				if(data.length > 0){
					dataCache = [];
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" ){
								data[i][key] = "";
							}
						}
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-headimg\">" +
								//"<img src=\"../../../../../../smartSchoolService"+data[i].headimg+"\" width=\"100%\" height=\"100%\"/>" +
							"</div>" +
							"<div class=\"tables-left\">" +
								"<div class=\"table-name\" style=\"width:140px;\" title=\""+data[i].name+"\"\>"+substr(data[i].name,10)+"</div>" +
								"<div class=\"table-code\" style=\"width:140px;\">"+getFormatDateByLong(parseInt(data[i].idatetime)*1000,"yyyy-MM-dd hh:mm")+"</div>" +
							"</div>" +
							"<div class=\"tables-middle\">" +
								"<div class=\"table-class\" style=\"width:400px;\" title=\""+data[i].content+"\">"+substr(data[i].content,30)+"</div>" +
								"<div class=\"table-code\" style=\"width:400px;\" ></div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\" >"+""+"</div>" +
								"<div class=\"table-oprate\">" +
									"<div class=\"table-detail\" dataindex = \""+i+"\"  title=\"详情\"></div>" +
								"</div>" +
							"</div>" +
						"</div>";
						dataCache.push(data[i]);
					}
					$("#anews-data-show").empty();
					$("#anews-data-show").append(content);
					
					$(".table-trs").unbind("hover");
					$(".table-trs").hover(function(){
						$(this).find(".table-oprate").show();
					},function(){
						$(".table-oprate").hide();
					});
					
					$(".table-detail").unbind("click");
					$(".table-detail").click(function(){
						$("#anews-detailform").show();
						$("#right-anews-tables").hide();
						var index = parseInt($(this).attr("dataindex"));
						$("#anews-detailform").contents().find(".news-div div").attr("contenteditable","false");
						$("#anews-detailform").contents().find(".news-div div").css("background-color","#E5E5E5");
						$("#anews-detailform").contents().find("input").attr("disabled","disabled");
						$("#anews-detailform").contents().find("input").css("background-color","#E5E5E5");
						
						$("#anews-detailform").contents().find("#back-to-table").unbind("click");
						$("#anews-detailform").contents().find("#back-to-table").click(function(){
							$("#anews-detailform").hide();
							$("#right-anews-tables").show();
						});
						showData(dataCache[index]);
					});
					
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#anews-data-show").empty();
					$("#anews-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#anews-data-show").empty();
				$("#anews-data-show").append(content);
			}
		},null,null);	
	}
	function showData(data){
		$("#anews-detailform").contents().find("#anews-name").val(data.name);
		$("#anews-detailform").contents().find("#anews-idatetime").val(getFormatDateByLong(parseInt(data.idatetime)*1000,"yyyy-MM-dd hh:mm"));
		$("#anews-detailform").contents().find("#anews-content").text(data.content);
//		for(key in data){
//			if($("#anews-detailform").contents().find("#anews-" + key).length != 0){
//				if($("#anews-detailform").contents().find("#anews-" + key)[0].tagName.toLowerCase() == "input"){
//					$("#anews-detailform").contents().find("#anews-" + key).val(data[key]);
//				}else{
//					$("#anews-detailform").contents().find("#anews-" + key).text(data[key]);
//				}
//			}
//		}
	}
	
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function isUndefined(param){
		if(param == undefined){
			param = "";
		}
		return param;
	}
	function pagebtns(){
		document.onkeydown = function(e){ 
			$("select").blur();
		    var ev = document.all ? window.event : e;
		    if(ev.keyCode==13) {
		    	now = 0;
		    	initNews(1,pageSize);
		    }
		};
		$("#anews-search-btn").click(function(){
			now = 0;
			initNews(1,pageSize);
		});
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();